package com.kehao.leetcode.jzoffer;

import org.junit.Test;

/**
 * @author Kehao.Chen
 * @version 1.0
 * @date 2022/6/23 17:19
 */
public class JZSolution028 {
    private static class Node {
        public int val;
        public Node prev;
        public Node next;
        public Node child;
        Node(int val){
            this.val = val;
        }
    };
    public Node flatten(Node head) {
        Node p = head;
        while (p!=null){
            if(p.child!=null){
                Node childPre = p.child;
                Node childLast = p.child;
                Node pNext = p.next;
                while (childLast.next!=null){
                    childLast=childLast.next;
                }
                p.child = null;
                p.next = childPre;
                childPre.prev = p;
                childLast.next = pNext;
                if(pNext!=null) pNext.prev = childLast;
            }
            p = p.next;
        }
        return head;
    }

    @Test
    public void test01(){
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        node1.child= node2;
        node2.child = node3;
        Node flatten = flatten(node1);
        System.out.println(flatten);
    }


}
